package com.sg.service.biz.framework.nb;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.sg.common.util.RedisUtil;
import com.sg.dto.biz.framework.req.*;
import com.sg.dto.biz.framework.res.*;
import com.wicket.okrcomponent.common.annotations.BanAuto;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ： 手工接入方法
 * @version 1.0
 * @since 2022/5/28 15:32
 */
@Service
public class NbBase {
    @Resource
    RedisUtil redisClient;

    /**
     * code:clearSpaceInfoFromRedis
     * name:Redis清空空间信息
     * desc:undefined
     **/
    @BanAuto
    @Trace(operationName = "Redis清空空间信息")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public ClearSpaceInfoFromRedisRespDto clearSpaceInfoFromRedis(ClearSpaceInfoFromRedisReqDto reqDto) {
        ClearSpaceInfoFromRedisRespDto retData = new ClearSpaceInfoFromRedisRespDto();
        if (reqDto.getSpaceId() == null) {
            return retData;
        }
        redisClient.delete(reqDto.getSpaceId());
        return retData;
    }

    /**
     * code:getRootCode
     * name:M1-获取组织标识
     * desc:undefined
     **/
    @BanAuto
    @Trace(operationName = "M1-获取组织标识")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public GetOrgCodeRespDto getOrgCode(GetOrgCodeReqDto reqDto) {
        GetOrgCodeRespDto retData = new GetOrgCodeRespDto();
        List<String> orgList = reqDto.getOrgList();
        List<Long> orgNumList = orgList.stream().map(org -> Long.valueOf(org)).collect(Collectors.toList());
        Long max = Collections.max(orgNumList);
        max = max + 1;
        retData.setOrganizationCode(String.valueOf(max));
        return retData;
    }

    /**
     * code:getCurrentContentCode
     * name:M-获取当前内容标识（特殊方法）
     * desc:undefined
     **/
    @Trace(operationName = "M-获取当前内容标识（特殊方法）")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public AddGetCurrentCodeRespDto addGetCurrentCode(AddGetCurrentCodeReqDto reqDto) {
        AddGetCurrentCodeRespDto retData = new AddGetCurrentCodeRespDto();
        if (reqDto.getPeerContentCodeList() == null || reqDto.getPeerContentCodeList().size() == 0) {
            if (reqDto.getSuperiorContentCode().equals("1")) {
                retData.setCurrentContentCode(Long.valueOf(reqDto.getSuperiorContentCode() + "001"));
            } else {
                retData.setCurrentContentCode(Long.valueOf(reqDto.getSuperiorContentCode() + "1001"));
            }

        } else if (reqDto.getPeerContentCodeList().size() > 0) {
            String first = reqDto.getPeerContentCodeList().get(0);
            String preFix = first.substring(0, first.length() - 4);
            List<String> shortCodeList = reqDto.getPeerContentCodeList().stream().map(code -> code.substring(code.length() - 4)).collect(Collectors.toList());
            List<Long> codeNumList = shortCodeList.stream().map(code -> Long.valueOf(code)).collect(Collectors.toList());
            Long max = Collections.max(codeNumList) + 1;
            retData.setCurrentContentCode(Long.valueOf(preFix + String.valueOf(max)));
        }
        return retData;
    }

    /**
     * code:batchClearSpaceInfoFromRedis
     * name:M-批量Redis清空空间信息（特殊方法）
     * desc:undefined
     **/
    @Trace(operationName = "M-批量Redis清空空间信息（特殊方法）")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public BatchClearSpaceInfoFromRedisRespDto batchClearSpaceInfoFromRedis(BatchClearSpaceInfoFromRedisReqDto reqDto) {
        BatchClearSpaceInfoFromRedisRespDto retData = new BatchClearSpaceInfoFromRedisRespDto();
        if (CollUtil.isEmpty(reqDto.getSpaceInfoToRedisList())) {
            return retData;
        }
        for (String oneSpace : reqDto.getSpaceInfoToRedisList()) {
            redisClient.delete(oneSpace);
        }
        return retData;
    }

    /**
     * code:receptionService
     * name:M1-获取接收字段（专门接收字段用于前端全部入参）
     * desc:undefined
     * gen by moon at 8/25/2022, 6:28:28 AM
     **/
    @Trace(operationName = "M1-获取接收字段（专门接收字段用于前端全部入参）")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    public QueryReceiveFieldDetailRespDto queryReceiveFieldDetail(QueryReceiveFieldDetailReqDto reqDto) {
        return BeanUtil.toBean(reqDto, QueryReceiveFieldDetailRespDto.class);
    }
    //手工接入方法
}
